package com.jason.platform.supervision.activity;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import com.baidu.mapapi.map.*;
import com.baidu.mapapi.model.LatLng;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.beans.HistoryResp;
import com.jason.platform.supervision.beans.LocateBean;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: BaiduMapActivity
 * @Description: 百度地图
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-8-17 16:23:54
 */
public class BaiduMapActivity extends BaseActivity {

    private static final String TAG = BaiduMapActivity.class.getSimpleName();

    public static final String ACTION_LOCATE_LOOK = "action_locate_look";
    public static final String ACTION_LOCATE_PLAYBACK = "action_locate_playback";
    public static final String KEY_LOCATE_LOOK = "key_locate_look";
    public static final String KEY_LOCATE_PLAYBACK = "key_locate_playback";

    private TitleBar mTitleBar;
    private MapView mMapView;

    private BaiduMap mBaiduMap;

    private LocateBean mLookLocateBean;
    private String mAction;
    private List<LocateBean> mLocateBeans = new ArrayList<LocateBean>();
    private List<LatLng> mLatLngs = new ArrayList<LatLng>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_map);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
        mMapView = (MapView) findViewById(R.id.map_mapview);
    }

    @Override
    protected void initData() {
        super.initData();
        mBaiduMap = mMapView.getMap();
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        mAction = getIntent().getAction();
        if (ACTION_LOCATE_LOOK.equals(mAction)) {
            mLookLocateBean = (LocateBean) getIntent().getSerializableExtra(KEY_LOCATE_LOOK);
            updateLookView(mLookLocateBean);
        } else if (ACTION_LOCATE_PLAYBACK.equals(mAction)) {
            HistoryResp resp = (HistoryResp) getIntent().getSerializableExtra(KEY_LOCATE_PLAYBACK);
            List<LocateBean> beans = resp.getValues();
            if (beans != null && !beans.isEmpty()) {
                Log.i(TAG, "count:" + beans.size());
                for (int i = 0; i < beans.size(); i++) {
                    LocateBean bean = beans.get(i);
                    int resource = R.drawable.ic_map_locate;
                    if (i == 0) {
                        resource = R.drawable.ic_map_start;
                    } else if (i == beans.size() - 1) {
                        resource = R.drawable.ic_map_end;
                    } else {
                        resource = R.drawable.ic_map_locate;
                    }
                    Log.i(TAG, "******position:" + i + ",******" + bean.toString());
                    if (bean != null && bean.isOpenMap()) {
                        mLatLngs.add(new LatLng(Double.parseDouble(bean.getLatitude()), Double.parseDouble(bean.getLongitude())));
                        OverlayOptions overlayOptions = new MarkerOptions().
                                position(new LatLng(Double.parseDouble(bean.getLatitude()), Double.parseDouble(bean.getLongitude()))).
                                icon(BitmapDescriptorFactory
                                        .fromResource(resource))
                                .zIndex(i).title(bean.getPositionInfo());
                        mBaiduMap.addOverlay(overlayOptions);
                        mLocateBeans.add(bean);
                    }
                }
                //化多边形
                OverlayOptions ooPolygon = new PolygonOptions().points(mLatLngs)
                        .stroke(new Stroke(5, 0xffff0000)).fillColor(0x00ffffff);
                mBaiduMap.addOverlay(ooPolygon);

                //定位
                MapStatus mMapStatus = new MapStatus.Builder()
                        .target(mLatLngs.get(0))
                        .build();
                //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
                MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
                //改变地图状态
                mBaiduMap.setMapStatus(mMapStatusUpdate);

                //添加浮层点击事件
                mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(Marker marker) {
                        TextView popupText = new TextView(BaiduMapActivity.this);
                        popupText.setWidth(400);
                        popupText.setBackgroundResource(R.drawable.ic_map_popup);
                        popupText.setTextColor(0xff000000);
                        popupText.setText(marker.getTitle());
                        mBaiduMap.showInfoWindow(new InfoWindow(popupText, marker.getPosition(), -47));
                        return false;
                    }
                });

            }

        }

    }


    //查看位置
    private void updateLookView(LocateBean bean) {
        LatLng point = new LatLng(Double.parseDouble(bean.getLatitude()), Double.parseDouble(bean.getLongitude()));

        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

//        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.ic_map_locate);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);

        MapStatus mMapStatus = new MapStatus.Builder()
                .target(point)
                .zoom(16)
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化

        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);

        TextView popupText = new TextView(this);
        popupText.setWidth(400);
        popupText.setBackgroundResource(R.drawable.ic_map_popup);
        popupText.setTextColor(0xff000000);
        popupText.setText(bean.getPositionInfo());
        mBaiduMap.showInfoWindow(new InfoWindow(popupText, point, -47));
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {

    }
}
